﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.ProcessEngine;
using VAdvantage.Utility;
using VAdvantage.Logging;
using System.Data.SqlClient;
using System.Data;
using VAdvantage.DataBase;
using System.Collections.Generic;
using VAdvantage.Model;
using System.Text;

namespace VAdvantage.Process
{
    /// <summary>
    /// Xml Generator process for records which are to be exported
    /// </summary>
    public class GenerateXmlForExportData : SvrProcess
    {
        private int _AD_ModuleInfo_ID = 0;

        List<ExportDataRecords> _ExportRecordList = new List<ExportDataRecords>();

        private DataSet ds;

        /// <summary>
        /// Prepare any parameter to be passed
        /// </summary>
        protected override void Prepare()
        {
            ProcessInfoParameter[] para = GetParameter();
            for (int i = 0; i < para.Length; i++)
            {
                String name = para[i].GetParameterName();
                if (para[i].GetParameter() == null)
                {
                    ;
                }
                else if (name.Equals("AD_ModuleInfo_ID"))
                {
                    _AD_ModuleInfo_ID = Util.GetValueOfInt(para[i].GetParameter());
                }
                else
                {
                    log.Log(Level.SEVERE, "Unknown Parameter: " + name);
                }
            }
        }

        /// <summary>
        /// Executes the process
        /// </summary>
        /// <returns></returns>
        protected override string DoIt()
        {


            foreach (ExportDataRecords exportdata in _ExportRecordList)
            {
                MTable currentTable = MTable.Get(GetCtx(), exportdata.AD_Table_ID);
                if (currentTable == null)   //should not be null
                {
                    continue;   //skip the record and move further for next record
                }

                if (currentTable.Get_ID() == 0) //should not be 0
                {
                    log.Log(Level.SEVERE, "Table record not found. Continuing with next record");
                    continue;   //move next
                }

                GetForeignData(currentTable, exportdata);
            }
            return "";
        }

        /// <summary>
        /// Gets the parent record for every row (if exists)
        /// </summary>
        /// <param name="currentTable">Current table to be parsed</param>
        /// <param name="exportdata">Exportdata info</param>
        private void GetForeignData(MTable currentTable, ExportDataRecords exportdata)
        {
            DataTable table = new DataTable();  //Add datatable which wil be inserted in dataset for xml gen.

            MColumn[] columns = currentTable.GetColumns(true); //Fetch column details
            if (columns.Length > 0)
            {
                DataSet tmpDS = DB.ExecuteDataset(GetSql(currentTable.GetTableName(), exportdata.Record_ID), null);
                table = tmpDS.Tables[0];
                table.TableName = currentTable.GetTableName();

                for (int cols = 0; cols <= columns.Length - 1; cols++)
                {
                    string colName = columns[cols].GetColumnName();
                    int refVID = columns[cols].GetAD_Reference_Value_ID();
                    int refID = columns[cols].GetAD_Reference_ID();

                    if (!columns[cols].IsStandardColumn() && !columns[cols].IsKey())
                    {
                        if (colName.EndsWith("_ID"))    //only columns ending with _ID to be processed (indicated Foreign Key )
                        {

                        }
                    }
                }
            }
        }


        String GetSql(String _tableName, int ID)
        {
            String _Sql = "SELECT * FROM " + _tableName + " WHERE IsActive = 'Y' AND " + _tableName + "_ID = " + ID;
            return _Sql;
        }

        #region Fetch ExportData

        /// <summary>
        /// Gets the Records to be exported into Xml
        /// </summary>
        /// <returns>Records in list</returns>
        List<ExportDataRecords> GetExportData()
        {
            List<ExportDataRecords> list = new List<ExportDataRecords>();

            String _sql = "SELECT AD_Table_ID, Record_ID FROM AD_ExportData WHERE IsActive = 'Y' AND AD_ModuleInfo_ID = @AD_ModuleInfo_ID";

            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@AD_ModuleInfo_ID", _AD_ModuleInfo_ID);

            IDataReader dr = DB.ExecuteReader(_sql);

            while (dr.Read())
            {
                ExportDataRecords records = new ExportDataRecords();
                records.AD_Table_ID = int.Parse(dr["AD_Table_ID"].ToString());
                records.Record_ID = int.Parse(dr["Record_ID"].ToString());

                list.Add(records);
            }
            dr.Close();


            return list;
        }

        #endregion

    }


    public class XmlFile
    {
        private String _Xml_FileName = "";
    }

    /// <summary>
    /// Stores record info which are to be exported into Xml
    /// </summary>
    public class ExportDataRecords
    {
        private int _AD_Table_ID = 0;

        /// <summary>
        /// Stores the table ID to be exported
        /// </summary>
        public int AD_Table_ID
        {
            get { return _AD_Table_ID; }
            set { _AD_Table_ID = value; }
        }

        private int _Record_ID = 0;

        /// <summary>
        /// Stores the Record ID (Primay Key) to be exported
        /// </summary>
        public int Record_ID
        {
            get { return _Record_ID; }
            set { _Record_ID = value; }
        }
    }
}
